home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 422_02 / dosutil / chattr.c < prev    next >
C/C++ Source or Header  |  1994-03-20  |  2KB  |  81 lines

  1. /*
  2.  * Change attributes of files
  3.  *
  4.  * Options:
  5.  *        +/-A    = ADD/REMOVE Archive bit
  6.  *        +/-H    = ADD/REMOVE Hidden bit
  7.  *        +/-R    = ADD/REMOVE Readonly bit
  8.  *        +/-S    = ADD/REMOVE System bit
  9.  *    Examples:
  10.  *        chattr \dos\*.* +r
  11.  *        chattr \temp\*.* -a
  12.  *        chattr c:\command.com +h +r
  13.  *
  14.  * Copyright 1990-1994 Dave Dunfield
  15.  * All rights reserved.
  16.  *
  17.  * Permission granted for personal (non-commercial) use only.
  18.  *
  19.  * Compile command: cc chattr -fop
  20.  */
  21. #include <stdio.h>
  22. #include <file.h>
  23.  
  24.     int or_mask = 0, and_mask = 0x0027;
  25.  
  26.     int fcount = 0;
  27.     char *fnames[20];
  28.  
  29.     char attributes[] = { 'R', 'H', 'S', 'V', 'D', 'A' };
  30.  
  31. /*
  32.  * Main program - set file attributes
  33.  */
  34. main(argc, argv)
  35.     int argc;
  36.     char *argv[];
  37. {
  38.     int i, j;
  39.     char chr, chr1, *ptr, *ptr1, path[65];
  40.  
  41.     char name[14];
  42.     int sizeh, sizel, attrs, time, date;
  43.  
  44.     /* Evaluate filenames & attributes to set/clear */
  45.     chr1 = 0;
  46.     for(i=1; i < argc; ++i) {
  47.         chr = argv[i][0];
  48.         if((chr == '+') || (chr == '-')) {
  49.             for(j=0; (chr1 = attributes[j]) != toupper(argv[i][1]); ++j) {
  50.                 if(!chr1)
  51.                     abort("Invalid attribute\n"); }
  52.             j = 1 << j;
  53.             if(chr == '+')
  54.                 or_mask |= j;
  55.             else
  56.                 and_mask &= ~j; }
  57.         else
  58.             fnames[fcount++] = argv[i]; }
  59.  
  60.     if(!(fcount && chr1))
  61.         abort("\nUse: chattr <filespec*>... +/-AHRS\n\nCopyright 1990-1994 Dave Dunfield\nAll rights reserved.\n");
  62.  
  63.     /* Step through file list, setting attributes of files */
  64.     for(i = 0; i < fcount; ++i) {
  65.         if(find_first(ptr = ptr1 = fnames[i], -1, name, &sizeh, &sizel, &attrs, &time, &date)) {
  66.             fputs(ptr, stderr);
  67.             fputs(": Not found\n", stderr);
  68.             continue; }
  69.         /* Determine directory path */
  70.         while(chr = *ptr++)
  71.             if((chr == '\\') || (chr == ':'))
  72.                 ptr1 = ptr;
  73.         *ptr1 = 0;
  74.         do {
  75.             concat(path, fnames[i], name);    /* Create full pathname */
  76.             if(set_attr(path, (attrs & and_mask) | or_mask)) {
  77.                 fputs(path, stderr);
  78.                 fputs(": Unable to set attributes\n", stderr); } }
  79.         while(!find_next(name, &sizeh, &sizel, &attrs, &time, &date)); }
  80. }
  81.